package com.cake.liulanxiangzhu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cake.liulanxiangzhu.exception.ServiceException;
import com.cake.liulanxiangzhu.pojo.dto.AdminBannerDTO;
import com.cake.liulanxiangzhu.pojo.dto.AdminBannerInsertDTO;
import com.cake.liulanxiangzhu.pojo.entity.AdminBanner;
import com.cake.liulanxiangzhu.pojo.vo.AdminBannerVO;
import com.cake.liulanxiangzhu.service.AdminBannerService;
import com.cake.liulanxiangzhu.mapper.AdminBannerMapper;
import com.cake.liulanxiangzhu.utils.DataUseUtil;
import com.cake.liulanxiangzhu.web.ServiceCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【admin_banner】的数据库操作Service实现
 * @createDate 2023-01-13 13:39:31
 */
@Service
public class AdminBannerServiceImpl extends ServiceImpl<AdminBannerMapper, AdminBanner>
        implements AdminBannerService {
    @Autowired
    AdminBannerMapper adminBannerMapper;

    @Override
    public void addNew(AdminBannerInsertDTO adminBannerInsertDTO) {
        AdminBanner adminBanner = new AdminBanner();
        BeanUtils.copyProperties(adminBannerInsertDTO, adminBanner);
        adminBanner.setCreatTime(new Date());
        adminBanner.setUpdateTime(new Date());
        adminBannerMapper.insert(adminBanner);
    }

    @Override
    public void deleteByID(Integer bannerId) {
        AdminBanner adminBanner = adminBannerMapper.selectById(bannerId);
        if (adminBanner == null) {
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "没有相关数据");
        }
        String bannerIcon = adminBanner.getBannerIcon();
        new File("e:/files" + bannerIcon).delete();
        int id = adminBannerMapper.deleteById(bannerId);
        if (id != 1) {
            throw new ServiceException(ServiceCode.ERR_DELETE, "系统繁忙请稍后再试");
        }
    }

    @Override
    public void updateById(AdminBannerDTO adminBannerDTO) {
        System.out.println("adminBannerDTO = " + adminBannerDTO);
        Integer bannerId = adminBannerDTO.getBannerId();
        String bannerName = adminBannerDTO.getBannerName();
        AdminBanner vo = adminBannerMapper.selectBannerIconByBannerId(bannerId);
        if (vo != null) {
            String bannerIcon = adminBannerDTO.getBannerIcon();
            String voBannerIcon = vo.getBannerIcon();
            if (DataUseUtil.isNotBlank(bannerIcon)) {
                if (!bannerIcon.equals(voBannerIcon)) {
                    new File("e:/files" + voBannerIcon).delete();
                }
            }
            if (DataUseUtil.isNotBlank(bannerName)) {
                int num = adminBannerMapper.countByBannerName(bannerName);
                if (num == 0) {
                    updateBanner(adminBannerDTO);
                } else {
                    if (DataUseUtil.isNotBlank(bannerIcon)) {
                        new File("e:/files" + bannerIcon).delete();
                    }
                    throw new ServiceException(ServiceCode.ERR_BAD_REQUEST, "轮播图名字重复了");
                }
            } else {
                updateBanner(adminBannerDTO);
            }
        } else {
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "没有相关数据");
        }
    }

    public void updateBanner(AdminBannerDTO adminBannerDTO) {
        AdminBanner banner = new AdminBanner();
        BeanUtils.copyProperties(adminBannerDTO, banner);
        banner.setUpdateTime(new Date());
        int i = adminBannerMapper.updateById(banner);
        if (i != 1) {
            throw new ServiceException(ServiceCode.ERR_DELETE, "系统繁忙，请稍后再试");
        }
    }

    @Override
    public List<AdminBannerVO> listBanner() {
        List<AdminBannerVO> vos = new ArrayList<>();
        List<AdminBanner> adminBanners = adminBannerMapper.selectList(null);
        for (AdminBanner adminBanner : adminBanners) {
            AdminBannerVO vo = new AdminBannerVO();
            BeanUtils.copyProperties(adminBanner, vo);
            vos.add(vo);
        }
        return vos;
    }

    @Override
    public AdminBannerVO queryBanner(Integer bannerId) {
        AdminBanner adminBanner = adminBannerMapper.selectById(bannerId);
        if (adminBanner == null) {
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "没有相关数据");
        }
        AdminBannerVO vo = new AdminBannerVO();
        BeanUtils.copyProperties(adminBanner, vo);
        return vo;
    }


}




